package mathive.client.panels;


import mathive.client.ClientGamesData;
import mathive.client.ClientTest;
import mathive.client.GameParent;
import mathive.client.TestService;
import mathive.client.TestServiceAsync;
import mathive.client.UserHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PlayTestPanel extends VerticalPanel{
	private static final TestServiceAsync SERVICE = GWT.create(TestService.class);
	
	private VerticalPanel gamePanel = new VerticalPanel();
	private ClientGamesData[] games;
	private int currentGame;
	private Button nextButton;
	
	private GameParent lastGame;
	
	private int testScore;
	private String name;
	private String statistics = "";
	
	public PlayTestPanel(String name) {
		this.name = name;
		add(new Label("Loading test..."));
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		SERVICE.getATest(
				name,
				new GetTestResponse());
	}
	
	private void nextGame() {
		if(currentGame >= 0){
			statistics = statistics + " " + Integer.toString(lastGame.getScore());
		}
		currentGame++;
		gamePanel.clear();
		if (currentGame < games.length) {
			ClientGamesData game = games[currentGame];
			
			if (game.getType().equals("Sudoku")) {
				SudokuPanel sudokuPanel = new SudokuPanel();
				lastGame = sudokuPanel;
				gamePanel.add(sudokuPanel.getPanel(game.getId(), game.getDifficulty()));
			} else if (game.getType().equals("Minemap")) {
				MinemapPanel minemapPanel = new MinemapPanel();
				lastGame = minemapPanel;
				gamePanel.add(minemapPanel.getPanel(game.getDifficulty()));
			} else if (game.getType().equals("Arithmetics")) {
				ArithmeticsPanel arithPanel = new ArithmeticsPanel();
				lastGame = arithPanel;
				gamePanel.add(arithPanel.getPanel(game.getDifficulty()));
			}
		} else {
			saveStatistics();
			remove(nextButton);
			add(new Label("Congratulations on finishing the test!"));
			UserHandler.singleton.addScore(testScore);
		}
	}
	
	private class GetTestResponse implements AsyncCallback<ClientTest> {
		@Override
		public void onFailure(Throwable caught) {
		}

		@Override
		public void onSuccess(ClientTest test) {
			clear();
			currentGame = -1;
			games = test.getGames();
			testScore = test.getScore();
			
//			add(new HTML("<h1>" + test.getName() +"</h1>"));
			add(gamePanel);
			
			nextButton = new Button("Next Puzzle");
			nextButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					nextGame();
				}
			});
			
			add(nextButton);
			nextGame();
		}
	}
	

	private void saveStatistics(){
		if (UserHandler.singleton.isOnline()) {
			// Set up the callback object.
			AsyncCallback<String> callback = new AsyncCallback<String>() {
				public void onFailure(Throwable caught) {
					Window.alert("The server malfunctioned. " +
							"Your score was saved but not added to statistics.");
				}
	
				public void onSuccess(String string) {
					Window.alert("Stats were saved.");
				}
			};
	
			// Make the call to the fetch sudoku service.
			SERVICE.addStatistics(name, UserHandler.singleton.getSessionID(), statistics, callback);
		}
	}
}
